//
//  BSSeeBigPictureViewController.m
//  BaiSi
//
//  Created by Sycamore on 16/5/5.
//  Copyright © 2016年 Sycamore. All rights reserved.
//

#import "BSSeeBigPictureViewController.h"
#import <UIImageView+WebCache.h>
#import "BSThemeItem.h"
#import <Photos/Photos.h>
#import <SVProgressHUD/SVProgressHUD.h>
#import "BSPhotoManager.h"

static NSString * const albumTitle = @"百思得姐";
@interface BSSeeBigPictureViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollContentView;
@property (nonatomic,strong) UIImage *image;
@property (nonatomic,weak) UIImageView *imageView;

@end

@implementation BSSeeBigPictureViewController

//返回
- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

//保存图片
- (IBAction)save:(id)sender {
    
    [BSPhotoManager saveImageToPhotoAlbumWithAlbumTitle:albumTitle image:_image completionHandler:^(BOOL success, NSError *error) {
        if (success) {
            //提示保存成功
            [SVProgressHUD showSuccessWithStatus:@"保存成功"];
        }
        else{
            //提示保存失败
            [SVProgressHUD showErrorWithStatus:@"保存失败"];
        }

    } deniedAuthorizationSatatusHandler:^{
        //提示用户去进行授权
        [SVProgressHUD showErrorWithStatus:@"请前往设置－>百思得姐－>授权"];
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建imageView，添加到scrollView上面去
    UIImageView *imageView = [[UIImageView alloc]init];
    [_scrollContentView addSubview:imageView];
    _imageView = imageView;
    imageView.userInteractionEnabled = YES;
    //1  viewDidLoad方法前，已经传好值了，也就是在加载view之前，控制器的item里面已经赋好值了（对于控制器可以这样，控件一般重写set方法）
    //2 sdwebImage内部已经对图片做好缓存了，所以这里图片并不会再次下载，而是从缓存里面去取的
    [imageView sd_setImageWithURL:[NSURL URLWithString:_item.image0] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        _image = image;
    }];
    
    //根据是大图还是小图进行显示
    imageView.bs_width = BSScreenWidth;
    imageView.bs_height = BSScreenWidth / _item.width * _item.height;
    //如果是小图，就是没有占据全屏的图片
    if (!_item.is_bigPicture) {
        
        imageView.center = self.view.center;
//        imageView.bs_width = BSScreenWidth;
        imageView.bs_X = 0;
    }
    else{
        //如果是大图，就要占据全屏而且能够滑动了，要设置scrollView能够滑动
        imageView.bs_X = 0;
        imageView.bs_Y = 0;
        
        _scrollContentView.contentSize = CGSizeMake(0, imageView.bs_height);
    }
    
    _scrollContentView.delegate = self;
    _scrollContentView.minimumZoomScale = 1.0f;
    if (_item.is_bigPicture) {
        _scrollContentView.maximumZoomScale = _item.height / imageView.bs_height;
    }else{
        _scrollContentView.maximumZoomScale = 1.0000001f;
    }
    
    
}


#pragma mark UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return  _imageView;
}



@end
